﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace DistictFinder
{
    public partial class frmDistict : Form
    {
        public frmDistict()
        {
            InitializeComponent();
        }

        private void frmDistict_Load(object sender, EventArgs e)
        {
            miscfun.Class1.loadStateData(comboBox1);
            miscfun.Class1.loadDrive(comboBox3);
            comboBox3.SelectedIndex = 0;
            DirectoryInfo dir = new DirectoryInfo(@comboBox3.Text);
            foreach(var dirname in dir.GetDirectories())
            {
                textBox2.AutoCompleteCustomSource.Add(dirname.ToString());
            }
        }
        private void listView1_Click(object sender, EventArgs e)
        {
            try
            {
                int selectedIndex = listView1.SelectedIndices[0];
                if (selectedIndex >= 0)
                {
                    //MessageBox.Show(listView1.Items[selectedIndex].Text);
                    miscfun.Class1.loadPostOffice(comboBox1.SelectedItem.ToString(), listView1.Items[selectedIndex].Text, comboBox2);
                    lblDistict.Text = listView1.Items[selectedIndex].Text;
                    miscfun.Class1.countPO(comboBox1.SelectedItem.ToString(), listView1.Items[selectedIndex].Text, toolStripStatusLabel2);
                    textBox1.Clear();
                }
            }
            catch (Exception exp)
            {

            }
        }

        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            miscfun.Class1.loadDistictData(listView1, comboBox1.SelectedItem.ToString());
            comboBox2.Items.Clear();
            comboBox2.Text = "";
            lblDistict.Text = "Distict Not Selected";
            miscfun.Class1.countDistict(comboBox1.SelectedItem.ToString(), toolStripStatusLabel1);
            textBox1.Clear();
            toolStripStatusLabel2.Text = "";
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            miscfun.Class1.displayPinCode(comboBox1.SelectedItem.ToString(), lblDistict.Text, comboBox2.SelectedItem.ToString(),textBox1);
            MessageBox.Show(pinCodeDetail().ToString());
        }
        public StringBuilder pinCodeDetail()
        {
            StringBuilder str = new StringBuilder();
            str.Append("State = ");
            str.Append(comboBox1.SelectedItem.ToString());
            str.Append("\n");
            str.Append("Distict = ");
            str.Append(lblDistict.Text);
            str.Append("\n");
            str.Append("Post Office = ");
            str.Append(comboBox2.SelectedItem.ToString());
            str.Append("\n");
            str.Append("Pin Code = ");
            str.Append(textBox1.Text);
            return str;
        }
        public void createFile(string path)
        {
            //string filename = @"D:\" + comboBox2.SelectedItem.ToString() + ".txt";
            StreamWriter objWriter = new StreamWriter(path);

            string[] aryTest = new string[4];
            aryTest[0] = "State = " + comboBox1.SelectedItem.ToString();
            aryTest[1] = "Distict = " + lblDistict.Text;
            aryTest[2] = "Post Office = " + comboBox2.SelectedItem.ToString();
            aryTest[3] = "Pin Code = " + textBox1.Text;

            for (int i = 0; i <= 3; i++)
            {
                objWriter.WriteLine(aryTest[i]);
            }
            objWriter.Close();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox2.Text.Length > 0)
            {
                string dirpath = @comboBox3.Text + textBox2.Text + @"\" + comboBox1.SelectedItem.ToString() + @"\" + lblDistict.Text;
                string res = miscfun.Class1.CreateRootDir(dirpath,comboBox1,lblDistict);
                DirectoryInfo dr = new DirectoryInfo(dirpath);
                if (dr.Exists)
                {
                    FileInfo fl = new FileInfo(dirpath + @"\" + comboBox2.SelectedItem.ToString() + ".txt");
                    if (!(fl.Exists))
                    {
                        createFile(dirpath + @"\" + comboBox2.SelectedItem.ToString() + ".txt");
                        MessageBox.Show("Result Save Successful");
                    }
                    else
                    {
                        MessageBox.Show("Result Already Save in" + Environment.NewLine + dirpath + @"\" + comboBox2.SelectedItem.ToString() + ".txt");
                    }
                }
            }
            else
            {
                MessageBox.Show("Enter Direcotry Name");
            }
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox2.Clear();
            textBox2.AutoCompleteCustomSource.Clear();
            DirectoryInfo dir = new DirectoryInfo(@comboBox3.Text);
            if (dir.Exists)
            {
                foreach (var dirname in dir.GetDirectories())
                {
                    textBox2.AutoCompleteCustomSource.Add(dirname.ToString());
                }
            }
            else
            {
                textBox2.AutoCompleteCustomSource.Clear();
            }
        }
        public void validdirname()
        {
            string specsymbol="/\\:*?\"<>|";
            char[] ch = specsymbol.ToCharArray();
            foreach (var specchar in ch)
            {
                MessageBox.Show(specchar.ToString());
            }

        }
    }
  
}
